package edu.pxxy.clm.daointerface.impl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


import java.util.ArrayList;
import java.util.List;

import edu.pxxy.clm.daointerface.FoodDao;
import edu.pxxy.clm.entity.Food;
import edu.pxxy.clm.daohelper.MyDBOpenHelper;

public class FoodImpl implements FoodDao {

    private MyDBOpenHelper myDBOpenHelper;
    private SQLiteDatabase sqLiteDatabase;

    public FoodImpl(Context context) {
        myDBOpenHelper = new MyDBOpenHelper(context);
        sqLiteDatabase = myDBOpenHelper.getWritableDatabase();
    }

    @Override
    public List<Food> findAllFood() {
        List<Food> foodList = new ArrayList<>();
        Cursor cursor = sqLiteDatabase.query("food", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            String foodname = cursor.getString(cursor.getColumnIndex("foodname"));
            String foodprice = cursor.getString(cursor.getColumnIndex("foodprice"));
            String count = cursor.getString(cursor.getColumnIndex("count"));
            Food food = new Food();
            food.setFoodname(foodname);
            food.setFoodprice(Double.parseDouble(foodprice));
            food.setCount(Integer.parseInt(count));
            foodList.add(food);
        }
        return foodList;
    }

    @Override
    public Food findFoodByName(String foodName) {
        Food food = new Food();
        Cursor cursor = sqLiteDatabase.query("food", null, "foodname=?", new String[]{foodName}, null, null, null);
        if (cursor.moveToFirst()) {
            String foodname = cursor.getString(cursor.getColumnIndex("foodname"));
            String foodprice = cursor.getString(cursor.getColumnIndex("foodprice"));
            String count = cursor.getString(cursor.getColumnIndex("count"));
            food.setFoodname(foodname);
            food.setFoodprice(Double.parseDouble(foodprice));
            food.setCount(Integer.parseInt(count));
        }
        return food;
    }

    @Override
    public Long insertFood(Food food) {
        ContentValues values = new ContentValues();
        values.put("foodname", food.getFoodname());
        values.put("foodprice", food.getFoodprice());
        values.put("count", food.getCount());
        long _id = sqLiteDatabase.insert("food", null, values);
        return _id;
    }

    @Override
    public int deleteFood(Integer _id) {
        int i = sqLiteDatabase.delete("food", "_id = ?", new String[]{_id + ""});
        return i;
    }

    @Override
    public int updateFoodPrice(Food food) {
        ContentValues values = new ContentValues();
        values.put("foodname", food.getFoodname());
        values.put("foodprice", food.getFoodprice());
        values.put("count", food.getCount());
        values.put("_id", food.get_id());
        int i = sqLiteDatabase.update("food", values, "foodname=?", new String[]{food.getFoodname()});
        return i;
    }
}
